package cn.qyd.concurrentcollection;

import java.util.concurrent.SynchronousQueue;

/**
 * @author 邱运铎
 * @date 2024-03-22 21:51
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<String> queue = new SynchronousQueue<>();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println("Putting Event SYNCHRONOUS_EVENT_"+i);
                    queue.put("SYNCHRONOUS_EVENT_"+i);
                    System.out.println("Successful Put Event SYNCHRONOUS_EVENT_"+i);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    String take = queue.take();
                    System.out.println("Taking Event : " + take);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
    }
}
